<template>
  <Page header="用户管理"  is-back>
    <view class="contents b-t">
      <view class="list b-b" v-for="(item, index) in addressList" :key="index" @click="checkAddress(item)">
        <view class="wrapper">
          <view class="address-box">
            <u-avatar show="circle" :src="item.avatar_url" default-url="/static/profile/jx216@4x.png" size="128"></u-avatar>
          </view>
          <view class="u-box">
            <div class="name">账号：{{item.username}}</div>
            <div class="mobile">手机：{{item.phone}}</div>
            <div class="mobile">角色：{{item.role === 1 ? '管理员' : '普通用户'}}</div>
            <div class="mobile">是否审批：{{item.is_audit === 1 ? '是' : '否'}}</div>
          </view>
        </view>
        <text class="" style="margin: 0 20rpx" @click.stop="addAddress('edit', item)">
          编辑
        </text>
        <text class="" style="color: #F82222" @click.stop="delAddress(item)">
          删除
        </text>
      </view>

      <button class="add-btn" @click="addAddress('add', {id: ''})">新增用户</button>
    </view>
  </Page>
</template>

<script>
	import Page from "../../components/Page.vue";

  export default {
    components: {Page},
		data() {
			return {
				source: 0,
				addressList: [

				]
			}
		},
		onShow(){
      this.getList()
		},
    onLoad({ source }) {
      this.source = source || 0
    },
		methods: {
      getList() {
        this.$api.userList({
        }).then(res => {
          this.addressList = res
        })
      },
      getRegionName(region) {
        return region ? JSON.parse(region).map(el => el.label).join('') : ''
      },
			//选择地址
			checkAddress(item){
				if(this.source == 1){
					//this.$api.prePage()获取上一页实例，在App.vue定义
					this.$prePage().address = item;
          // 上一页实例
					uni.navigateBack()
				}
			},
			addAddress(type, item){
				uni.navigateTo({
					url: `/pages/user/add?type=${type}&id=${item.id}`
				})
			},
      delAddress(item){
        let that = this
        // 确认对话框
        uni.showModal({
          title: '提示',
          content: '确定删除该用户吗？',
          success: (res) => {
            if (res.confirm) {
              that.$api.delUser(item.id).then(res => {
                that.getList()
              })
            }
          }
        })
      }
		}
	}
</script>

<style lang='scss'>
	.contents{
    height: 100%;
		position: relative;
	}
	.list{
		display: flex;
		align-items: center;
		padding: 20upx 30upx;;
		background: #fff;
		position: relative;
    border-bottom: 1px solid #e4e7ed;
	}
	.wrapper{
		display: flex;
		flex: 1;
    align-items: center;
	}
	.address-box{
		display: flex;
		align-items: center;
		.tag{
			font-size: 24upx;
			color: #17BDB2;
			margin-right: 10upx;
			background: #fffafb;
			border: 1px solid #17BDB2;
			border-radius: 4upx;
			padding: 4upx 10upx;
			line-height: 1;
		}
		.address{
			font-size: 30upx;
			color: $font-color-dark;
		}
	}
	.u-box{
		font-size: 28upx;
		color: $font-color-light;
		margin-top: 16upx;
    margin-left: 20rpx;
	}
	.icon-bianji{
		display: flex;
		align-items: center;
		height: 80upx;
		font-size: 40upx;
		color: $font-color-light;
		padding-left: 30upx;
	}

	.add-btn{
		position: fixed;
		left: 30upx;
		right: 30upx;
		bottom: 50upx;
		z-index: 95;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		font-size: 32upx;
		color: #fff;
		background-color: #17BDB2;
		border-radius: 10upx;
	}
</style>
